package com.yunsom.testdemo.multiple_thread.threadpooltest3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author 10400
 * @create 2018-04-19 20:38
 */
public class FutureTest {

/**
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(5, 200,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        for(int i=0;i<1000;i++) {
            pool.execute(() -> {
                //todo 业务逻辑在此
            });
        }
*/
    //1、配置线程池
    private static ExecutorService es = Executors.newFixedThreadPool(20);

    //2、封装响应Feature
    class BizResult{
        public String orderId;
        public String  data;

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }


    //3、实现Callable接口
    class BizTask implements Callable {

        private String orderId;

        private Object data;


        public BizTask(String orderId, Object data) {
            this.orderId = orderId;
            this.data = data;
        }

        @Override
        public Object call() {
            try {
                //todo business
                System.out.println("当前线程Id = " + this.orderId);
                BizResult br = new BizResult();
                br.setOrderId(this.orderId);
                br.setData("some key about your business" + this.getClass());
                Thread.sleep(3000);
                return br;
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }

    /**
     * 业务逻辑入口
     */
    public List<Future> beginBusiness() throws InterruptedException, ExecutionException {
        //模拟批量业务数据
        List<String> list = new ArrayList<>();
        for (int i = 0 ; i < 100 ; i++) {
            list.add(String.valueOf(i));
        }

        //接收多线程响应结果
        List<Future> resultList = new ArrayList<>();
        //begin thread
        for( int i = 0 ,size = list.size() ; i<size; i++){
            //todo something befor thread
            Future future = es.submit(new BizTask(list.get(i), null));
            resultList.add(future);
        }

        for (Future f : resultList) {
            f.get();
        }

        System.out.println(" =====多线程执行结束====== ");

        //wait finish
        return resultList;
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        FutureTest ft = new FutureTest();
            List<Future> futures = ft.beginBusiness();
            System.out.println("futures.size() = " + futures.size());
            //todo some operate
            System.out.println(" ==========================end========================= " );
    }

}

